
var Timer;
var userInter = false;
function setCurrentTab(id) {
    clearInterval(Timer);
    var buttons = Array.prototype.slice.call(document.querySelectorAll('.case-studies-nav a'));
    for (i = 0; i < buttons.length; i++) {
        buttons[i].classList.remove('active');
        buttons[i].classList.remove('active-animated');
    }

    var tabs = document.querySelectorAll('.hero-tab');
    for (i = 0; i < tabs.length; i++) {
        tabs[i].style.display = 'none';
    }

    var button = document.querySelector('[data-target=' + id + ']');
    button.classList.add('active');

    var tab = document.querySelector('[data-tab=' + id + ']');
    tab.style.display = 'block';

    document.getElementById('hero').setAttribute('data-current-id', id);

    if (!userInter) {
        button.classList.add('active-animated');
        var idx = buttons.findIndex(function(btn) {
        return btn.getAttribute('data-target') === id;
        });
        idx += 1;

        var nextButton = idx >= buttons.length ? buttons[0] : buttons[idx];

        Timer = setTimeout(function () {
        setCurrentTab(nextButton.getAttribute('data-target'));
        }, 10000);
    }
}
window.addEventListener('load', function() {
    var tabs = document.querySelectorAll('.hero-tab');
    for (i = 0; i < tabs.length; i++) {
        document.querySelectorAll('.case-studies-nav a')[i].addEventListener('click', function(event) {
            event.preventDefault();
            userInter = true;
            var clickedTabButton = event.target || event.srcElement;
            var id = clickedTabButton.getAttribute('data-target');
            setCurrentTab(id);
        });
    }
})